package sms.controller;

import com.alibaba.excel.EasyExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sms.entity.Class;
import sms.entity.Course;
import sms.entity.Student_info;
import sms.service.ClassService;
import sms.service.CourseService;
import sms.service.ScoreService;
import sms.service.StudentInfoService;
import sms.vo.ScoreReport;
import sms.vo.UnqualifiedStudent;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    ClassService classService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private StudentInfoService studentInfoService;
    @Autowired
    private ScoreService scoreService;

    @GetMapping("/index")
    public String index() {
        return "admin/index";
    }

    //进入班级管理页面
    @GetMapping("/classes")
    public String classes(Model model) {
        List<Class> classes=classService.getClasses(null);
        model.addAttribute("classes",classes);
        model.addAttribute("clazz",new Class());
        return "admin/classes";
    }
    //添加班级
    @PostMapping("/classes")
    public String addClass(@ModelAttribute("clazz") Class clazz) {
        classService.addClass(clazz);
        return "redirect:/admin/classes";
    }
    //修改班级
    @PostMapping("/classes/edit")
    public String updateClass(@ModelAttribute("clazz") Class clazz) {
        classService.editClass(clazz);
        return "redirect:/admin/classes";
    }
    //删除班级
    @DeleteMapping("/classes/{id}")
    public String deleteClass(@PathVariable String id) {
        classService.deleteClass(id);
        return "redirect:/admin/classes";
    }
    //查询班级
    @GetMapping("/classes/search")
    public String searchClasses(@RequestParam(required = false)String id, Model model) {
        List<Class> classes=classService.getClasses(id);
        model.addAttribute("classes",classes);
        model.addAttribute("clazz",new Class());
        return "admin/classes";
    }
    //excel导入（待实现）
    /*@PostMapping("/import")
    public String importExcel(@RequestParam("file") MultipartFile file) {
        try {
            if (file.isEmpty()) {
                throw new IllegalArgumentException("上传文件不能为空");
            }

            // 读取Excel文件
            EasyExcel.read(file.getInputStream(), ClassExcelDTO.class,
                    new ClassExcelListener(classService)).sheet().doRead();

            return "redirect:/admin/classes?importSuccess=true";
        } catch (Exception e) {
            // 记录错误日志
            e.printStackTrace();
            return "redirect:/admin/classes?importError=true";
        }
    }*/

    //进入课程管理页面
    @GetMapping("/courses")
    public String courses(Model model) {
        List<Course> courses=courseService.getCourseList(null);
        model.addAttribute("courses",courses);
        model.addAttribute("course",new Course());
        return "admin/courses";
    }
    //添加课程
    @PostMapping("/courses")
    public String addCourse(@ModelAttribute("course") Course course) {
        courseService.addCourse(course);
        return "redirect:/admin/courses";
    }
    //查询课程
    @GetMapping("/courses/search")
    public String searchCourses(@RequestParam(required = false)String name, Model model) {
        List<Course> courses=courseService.getCourseList(name);
        model.addAttribute("courses",courses);
        model.addAttribute("course",new Course());
        return "admin/courses";
    }
    //修改课程
    @PostMapping("/courses/edit")
    public String updateCourse(@ModelAttribute("course") Course course) {
        courseService.updateCourse(course);
        return "redirect:/admin/courses";
    }

    @GetMapping("/reports")
    public String reports(Model model) {
        List<ScoreReport> scoreReports= scoreService.getClassScoreReport();
        model.addAttribute("scoreReports",scoreReports);
        List<UnqualifiedStudent> unqualifiedStudents=scoreService.getUnqualifiedStudentsByCourse();
        model.addAttribute("unqualifiedStudents",unqualifiedStudents);
        return "admin/reports";
    }

    //进入学生管理系统
    @GetMapping("/students")
    public String students(Model model) {
        List<Student_info> studentInfos=studentInfoService.searchStudentInfo(null,null,null);
        model.addAttribute("studentInfos",studentInfos);
        model.addAttribute("student",new Student_info());
        return "admin/students";
    }
    //添加学生
    @PostMapping("/students")
    public String addStudent(@ModelAttribute("student") Student_info student) {
        studentInfoService.addStudentInfo(student);
        return "redirect:/admin/students";
    }
    //查询学生
    @GetMapping("/students/search")
    public String searchStudent(@RequestParam(required = false)String id,@RequestParam(required = false)String name,@RequestParam(required = false)String sex, Model model) {
        List<Student_info> studentInfos=studentInfoService.searchStudentInfo(id,name,sex);
        model.addAttribute("studentInfos",studentInfos);
        model.addAttribute("student",new Student_info());
        return "admin/students";
    }
    //修改学生
    @PostMapping("/students/edit")
    public String updateStudent(@ModelAttribute("student") Student_info student) {
        studentInfoService.editStudentInfo(student);
        return "redirect:/admin/students";
    }

    // 其他方法可根据需求添加
}